
#include "stdafx.h"
#include "MonoCache.h"

CMonoCache::CMonoCache()
{
}

CMonoCache::~CMonoCache()
{
}

bool CMonoCache::GetCache(SinglePageDataMap& getCache)
{
	if(m_Cache.size() == 0){
		if(false == RefreshCache(true)){
			return false;
		}
	}
	
	getCache.clear();
	getCache = m_Cache;

	return true;

}

bool CMonoCache::GetAllDataFromDB(Tab1VarMap& tab1VarStructMap, Tab2VarMap& tab2VarStructMap, CList<Tab3ListVarStruct>& tab3ListVarStructList, CList<Tab4ListVarStruct>& tab4ListVarStructList)
{
	//get data from databases
	CString dbSourceName	= _T("MonoDB");
	CString dbName			= _T("MotleyMono");
	if(!m_ODBC.IsOpened()){
		if(!m_ODBC.Open(dbSourceName, /*_T("root")*/NULL, /*_T("zongtongyi")*/NULL)){
			return false;
		}
	}
	//init DataBase
	/*{
		//m_ODBC.SetDB(dbName);
		m_ODBC.InitCreateDB(dbName);
		m_ODBC.InitCreateTable(dbName);
		m_ODBC.Commit();
	}*/

	m_ODBC.GetTabDataFromDB(tab1VarStructMap);
	m_ODBC.GetTabDataFromDB(tab2VarStructMap);
	m_ODBC.GetTabDataFromDB(tab3ListVarStructList);
	m_ODBC.GetTabDataFromDB(tab4ListVarStructList);

	return true;
}

bool CMonoCache::RefreshCache(bool bFromDB)
{
	if(true == bFromDB){
		Tab1VarMap tab1VarStructMap;
		Tab2VarMap tab2VarStructMap;
		CList<Tab3ListVarStruct>	tab3ListVarStructList;
		CList<Tab4ListVarStruct>	tab4ListVarStructList;
		if(false == GetAllDataFromDB(tab1VarStructMap, tab2VarStructMap, tab3ListVarStructList, tab4ListVarStructList)){
			return false;
		}
		//SinglePageDataMap	m_CacheAllPageData
		FillIntoCache(tab1VarStructMap, tab2VarStructMap, tab3ListVarStructList, tab4ListVarStructList);
	}

	return true;
}

void CMonoCache::FillIntoCache(Tab1VarMap& tab1VarStructMap, Tab2VarMap& tab2VarStructMap, CList<Tab3ListVarStruct>& tab3ListVarStructList, CList<Tab4ListVarStruct>& tab4ListVarStructList)
{
	Tab1VarMap::iterator itrTab1 = tab1VarStructMap.begin();
	while(itrTab1 != tab1VarStructMap.end()){
		SinglePageData& pageData = m_Cache[itrTab1->first];
		pageData.tab1Data	= itrTab1->second;
		pageData.listOrder	= itrTab1->first;

		itrTab1++;
	}

	Tab2VarMap::iterator itrTab2 = tab2VarStructMap.begin();
	while(itrTab2 != tab2VarStructMap.end()){
		SinglePageData& pageData = m_Cache[itrTab2->first];
		pageData.tab2Data	= itrTab2->second;

		itrTab2++;
	}

	POSITION posTab3 = tab3ListVarStructList.GetHeadPosition();
	while(posTab3){
		Tab3ListVarStruct& tab3Data = tab3ListVarStructList.GetNext(posTab3);
		SinglePageData& pageData = m_Cache[tab3Data.id_SinglePageData];
		pageData.tab3DataList.AddTail(tab3Data);
	}

	POSITION posTab4 = tab4ListVarStructList.GetHeadPosition();
	while(posTab4){
		Tab4ListVarStruct& tab4Data = tab4ListVarStructList.GetNext(posTab4);
		SinglePageData& pageData = m_Cache[tab4Data.id_SinglePageData];
		pageData.tab4DataList.AddTail(tab4Data);
	}
}

void CMonoCache::AddIntoCache(int iNewPageId, const SinglePageData& newPageData)
{
	SinglePageData& pageData = m_Cache[iNewPageId];
	pageData = newPageData;
	AddIntoDatabase(iNewPageId, newPageData);
}

bool CMonoCache::AddIntoDatabase(int keyId, const SinglePageData& pageData)
{
	m_ODBC.AddTab1Data(keyId, pageData.tab1Data);
	m_ODBC.AddTab2Data(keyId, pageData.tab2Data);
	m_ODBC.AddTab3Data(keyId, pageData.tab3DataList);
	m_ODBC.AddTab4Data(keyId, pageData.tab4DataList);
	return true;
}

void CMonoCache::ModifyCache(int iModifyPageId, const SinglePageData& newPageData)
{
	SinglePageData& pageData = m_Cache[iModifyPageId];
	pageData = newPageData;
	ModifyDatabase(iModifyPageId, pageData.tab1Data);
	ModifyDatabase(iModifyPageId, pageData.tab2Data);
}

bool CMonoCache::ModifyDatabase(int keyId, const Tab1VarStruct& tab1)
{
	m_ODBC.ModifyTab1Data(keyId, tab1);
	return true;
}

bool CMonoCache::ModifyDatabase(int keyId, const Tab2VarStruct& tab2)
{
	m_ODBC.ModifyTab2Data(keyId, tab2);
	return true;
}



